package com.example.studytest.Controller.Researcher;

import com.alibaba.excel.EasyExcel;
import com.example.studytest.Entity.Test4.Record_score_info;
import com.example.studytest.Repository.Test4.Record_score_infoRepository;
import com.example.studytest.Util.CreateSheetUtil;
import com.example.studytest.Util.GeneralListener;
import com.example.studytest.Util.ObjectToListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/uploadExcel")
public class ExcelUploadControoler {

    @Autowired
    private Record_score_infoRepository record_score_infoRepository;

    @PostMapping("/importRecordInfo")
    public Boolean importCourse(@RequestParam("file") MultipartFile file) throws IOException{
//        1、使用实现ReadListener的方式
//        EasyExcel.read(file.getInputStream(),CourseInfo.class, new UploadDataListener(courseInfoMapper)).sheet().doRead();
//        2、使用继承AnalysisEventListener的方式
//        EasyExcel.read(file.getInputStream(), CourseInfo.class,new CourseInfoListener(courseService)).sheet().doRead();
//        3、使用继承AnalysisEventListener且使用泛型的方式：
//        3.1、建立一个通用的读取监听器，在该监听器读取到数据，并使用List保存，读取完成之后，使用getList()方法获得
        GeneralListener<Record_score_info> generalListener = new GeneralListener<>();
//        3.2、执行Excel的读取，.read()方法需要传入三个参数：1、文件。2、实体类。3、监听器。
        EasyExcel.read(file.getInputStream(),Record_score_info.class, generalListener).sheet().doRead();
//        3.3、获得监听器读取的数据列表
        List<Record_score_info> record_scoreInfoList = generalListener.getList();
        for (Object o : record_scoreInfoList) {
            Record_score_info item = (Record_score_info) o;
            System.out.println(item);
        }

        System.out.println(record_scoreInfoList.size());
        record_score_infoRepository.saveAll(record_scoreInfoList);
//        for(int i = 0; i<record_scoreInfoList.size();i++){
//            record_score_infoRepository.save(record_scoreInfoList.get(i));
//        }
//        3.4、执行后续入库操作
//        record_score_infoRepository.saveAll(record_scoreInfoList);
        return true;
    }

    @GetMapping("/generateTemplate")
    public void generateTemplate(@RequestParam String remail){
        record_score_infoRepository.insertInfoByRemail(remail); //先在表中生成模板数据
    }

    @GetMapping("/downloadTemplate")
    public void downloadTemplate(@RequestParam String remail, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Record_score_info> Infolist = record_score_infoRepository.getRSInfoByR_email(remail);
        String[] heads = ObjectToListUtil.getFieldsName(Infolist.get(0));
        //创建excel表头
        List<String> column = new ArrayList<>();
        for(int i=0;i<heads.length;i++){
            column.add(heads[i]);
        }

        //表头对应的数据
        List<Map<String,Object>> data = new ArrayList<>();

        for (int i = 0; i < Infolist.size(); i++) {
            Map<String,Object> dataMap = new HashMap<>();
            Record_score_info info_data = Infolist.get(i);
            dataMap.put("id",info_data.getId());
            dataMap.put("exp_date",info_data.getExp_date());
            dataMap.put("invited_code",info_data.getInvited_code());
            dataMap.put("session",info_data.getSession());

            data.add(dataMap);
        }
        CreateSheetUtil.exportExcel("template",column, data, request, response);

    }

}
